package com.epam.qanda.web.servlet.logged;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.epam.qanda.db.model.Answer;
import com.epam.qanda.db.model.User;
import com.epam.qanda.db.model.UserAnswerLike;
import com.epam.qanda.db.service.AnswerService;
import com.epam.qanda.db.service.UserAnswerLikeService;
import com.epam.qanda.db.service.UserService;

public class LikeAnswerServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String id = request.getParameter("answerId");
		String url = request.getServletPath();
		Integer answerId = Integer.parseInt(id);
		Answer answer = new AnswerService().read(answerId);
		User user = (User) request.getSession().getAttribute("user");
		User userAnswer = new UserService().read(answer.getUserId());
		if (user != null) {
			if (!user.isStatus()) {
				if (!user.getId().equals(answer.getUserId())) {
					UserAnswerLike userAnswerLike = new UserAnswerLikeService().getUserAnswer(
							user.getId(), answerId);
					if (userAnswerLike == null) {
						userAnswerLike = new UserAnswerLike();
						userAnswerLike.setUserId(user.getId());
						userAnswerLike.setAnswerId(answerId);
						if (url.equals("/increaseLikeAnswer")) {
							answer.setLike(answer.getLikes() + 1);
							userAnswerLike.setDirection(true);
							userAnswer.setRating(userAnswer.getRating() + 1);
							if (userAnswer.getRating() > (-20)) {
								userAnswer.setStatus(false);
							}
							new UserService().update(userAnswer);
						}
						if (url.equals("/decreaseLikeAnswer")) {
							answer.setLike(answer.getLikes() - 1);
							userAnswerLike.setDirection(false);
							userAnswer.setRating(userAnswer.getRating() - 1);
							if (userAnswer.getRating() <= (-20)) {
								userAnswer.setStatus(true);
							}
							new UserService().update(userAnswer);
						}
						new AnswerService().updateLikes(answer);
						new UserAnswerLikeService().create(userAnswerLike);
						response.getWriter().println(answer.getLikes());
					} else {
						if (userAnswerLike.getDirection() == true) {
							if (url.equals("/decreaseLikeAnswer")) {
								new UserAnswerLikeService().delete(userAnswerLike.getId());
								answer.setLike(answer.getLikes() - 1);
								new AnswerService().updateLikes(answer);
								userAnswer.setRating(userAnswer.getRating() - 1);
								if (userAnswer.getRating() <= (-20)) {
									userAnswer.setStatus(true);
								}
								new UserService().update(userAnswer);
								response.getWriter().println(answer.getLikes());
							} else {
								response.getWriter().println("You've already liked this answer! ");
							}
						} else {
							if (url.equals("/increaseLikeAnswer")) {
								new UserAnswerLikeService().delete(userAnswerLike.getId());
								answer.setLike(answer.getLikes() + 1);
								new AnswerService().updateLikes(answer);
								userAnswer.setRating(userAnswer.getRating() + 1);
								if (userAnswer.getRating() > (-20)) {
									userAnswer.setStatus(false);
								}
								new UserService().update(userAnswer);
								response.getWriter().println(answer.getLikes());
							} else {
								response.getWriter().println(
										"You've already disliked this answer! ");
							}
						}
					}
				} else {
					response.getWriter().println("You can't like or dislike your own answer!");
				}
			} else {
				response.getWriter().println(
						"You can't like or dislake answer, because your account is locked!");
			}

		} else {
			response.getWriter().println("");
		}
	}
}
